import { AddLog } from '@/apis/api'
import dayjs from 'dayjs'

export interface ILogger{
  track(message: string, module?: string) : Promise<void>
  info(message: string, module?: string) : Promise<void>
  error(message: string, module?: string) : Promise<void>
  warning(message: string, module?: string) : Promise<void>
}

declare let Logger : ILogger

export const Log : ILogger = {
  track: async (message: string, module = '系统消息') : Promise<void> => {
    console.log(`[${dayjs().format('YYYY-MM-DD HH:mm:ss')}][${module}][调试]:${message}`)
    await Logger.track(message, module)
    await AddLog({
      Info: message,
      Module: module,
      Level: '调试',
      CreateTime: new Date()
    })
  },
  info: async (message: string, module = '系统消息') : Promise<void> => {
    console.log(`[${dayjs().format('YYYY-MM-DD HH:mm:ss')}][${module}][消息]:${message}`)
    await Logger.info(message, module)
    await AddLog({
      Info: message,
      Module: module,
      Level: '信息',
      CreateTime: new Date()
    })
  },
  error: async (message: string, module = '系统消息') : Promise<void> => {
    console.log(`[${dayjs().format('YYYY-MM-DD HH:mm:ss')}][${module}][错误]:${message}`)
    await Logger.error(message, module)
    await AddLog({
      Info: message,
      Module: module,
      Level: '错误',
      CreateTime: new Date()
    })
  },
  warning: async (message: string, module = '系统消息') : Promise<void> => {
    console.log(`[${dayjs().format('YYYY-MM-DD HH:mm:ss')}][${module}][调试]:${message}`)
    await Logger.warning(message, module)
    await AddLog({
      Info: message,
      Module: module,
      Level: '警告',
      CreateTime: new Date()
    })
  }
}
